//
//  CommonBtn.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/4/19.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit
//typealias selectBlock = ()->()
class CommonBtn: UIButton {
//    var selectBlock:selectBlock?
    
    class func creatSelectBtnWithTitle(title:String,y:CGFloat,view:UIView,sel:Selector) -> UIButton {
        let btn:UIButton = UIButton(frame: CGRect(x: 20, y: y, width: kSCREEN_WIDTH-40, height: 40))
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        btn.setTitle(title, for: UIControlState.normal)
        btn.setTitleColor(UIColor(hexStr:"#ffffff"), for: UIControlState.normal)
        btn.addTarget(view, action: sel, for: UIControlEvents.touchUpInside)
        btn.layer.masksToBounds = true
        btn.layer.cornerRadius = 4
        CommonBtn.createImageWithColor(color: kThemeColor, btn: btn, state: UIControlState.normal)
        CommonBtn.createImageWithColor(color: kThemeColor.withAlphaComponent(0.7), btn: btn, state: UIControlState.highlighted)
        return btn
    }
    
    @objc func btnAction() -> Void {
        
    }
    
    class func createImageWithColor(color:UIColor, btn:UIButton, state:UIControlState) -> Void {
        let rect = CGRect(x: 0, y: 0, width: 10.0, height: 10.0)
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext();
        context!.setFillColor(color.cgColor);
        context!.fill(rect)
        let theImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        let imageNormal = theImage.resizableImage(withCapInsets: UIEdgeInsetsMake(2, 2, 2, 2))
        btn.setBackgroundImage(imageNormal, for: state)
    }
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
